<?php

/**
 * Library :: DAL (Data Access Layer) :: DataObject
 *
 * Warlock is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 *
 * @package    warlock
 * @author     David Cole <neophyte@sourcetutor.com>
 * @license    GNU/GPL v2
 */

// Object Dependencies
using('Engine.DAL.DataObject');

class DAL_DataObject_user extends DAL_DataObject
{
    var $_DAL;
    var $_Observer;

    function __construct()
    {
        parent::__construct();

        $this->_DAL      = &Vault::Get('DAL');
        $this->_Observer = &Vault::Get('Observer');

        // Table Properties
        $this->Table      = 'w_engine_users';
        $this->PrimaryKey = 'id';

        // Field Definitions
        $this->Fields['id']           = array('type' => 'INTEGER', 'size' => '10', 'unsigned' => true, 'autoincrement' => true);
        $this->Fields['gid']          = array('type' => 'INTEGER', 'size' => '4', 'unsigned' => true);
        $this->Fields['created']      = array('type' => 'DATETIME', 'autoinsert' => true);
        $this->Fields['status']       = array('type' => 'INTEGER', 'size' => '4', 'unsigned' => true);
        $this->Fields['username']     = array('type' => 'STRING', 'size' => '255');
        $this->Fields['email']        = array('type' => 'STRING', 'size' => '255');
        $this->Fields['password']     = array('type' => 'STRING', 'size' => '40');
        $this->Fields['name']         = array('type' => 'STRING', 'size' => '40');
        $this->Fields['display_name'] = array('type' => 'STRING', 'size' => '30');
        $this->Fields['theme']        = array('type' => 'STRING', 'size' => '255');
        $this->Fields['signature']    = array('type' => 'STRING', 'size' => '255');
        $this->Fields['timezone']     = array('type' => 'STRING', 'size' => '8');
        $this->Fields['language']     = array('type' => 'STRING', 'size' => '8');
        $this->Fields['avatar']       = array('type' => 'STRING', 'size' => '255');

        /*
         * Data Object Relationships
         */

        $this->Relationships[] = array('table'  => 'w_engine_groups',
                                       'do'     => 'group',
                                       'fields' => array('label' => 'group_label'),
                                       'relate' => array('gid' => 'id'));

        $this->Relationships[] = array('table'  => 'w_engine_profile_values',
                                       'do'     => 'profile',
                                       'type'   => 'CASCADE',
                                       'relate' => array('id' => 'uid'));
    }

    function Insert($data)
    {
        $data['password'] = $this->_GeneratePasswordHash($data['password']);
        $id               = $this->_DAL->DAO->Insert($this->_Name, $data);

        $this->_Observer->RaiseEvent('onStoreUser', array($id, $data));

        return $id;
    }

    function Delete($where)
    {
        $ret = $this->_DAL->DAO->Delete($this->_Name, $where);

        if (!is_scalar($where))
            return $ret;
        else
        {
            $this->_Observer->RaiseEvent('onDeleteUser');
            return $ret;
        }
    }

    function _GeneratePasswordHash($password)
    {
    	$seed = '';

		for ($i = 0; $i < 8; $i++)
			$seed .= substr('0123456789abcdef', rand(0, 15), 1);

		return md5($seed.$password).$seed;
    }
}

?>